package action;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Meeting;
import dao.impl.MeetingDAOImpl;

public class MeetingSearchAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MeetingDAOImpl md = new MeetingDAOImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		// 获取数据
		String topic = request.getParameter("meetingTopic");
		String participants = request.getParameter("meetingParticipants");
		String meetingPerson = request.getParameter("meetingPerson");
		String guestName = request.getParameter("guestName");
		String deptmentName = request.getParameter("deptmentName");
		String meetingType = request.getParameter("meetingType");
		String meetingState = request.getParameter("meetingState");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		// 构建HQL语句
		StringBuffer sb = new StringBuffer("from Meeting as m where 1=1 ");
		// 如果条件值 不为空，则将条件值拼接入SQL中, m.xx其中xx代表类成员变量名
		if (!"".equals(topic) && topic!=null) {
			sb.append(" and m.topic like '%");
			sb.append(topic);
			sb.append("%' ");
		}
		if (!"".equals(participants) && participants!=null) {
			sb.append(" and m.participants like '%");
			sb.append(participants);
			sb.append("%' ");
		}
		if(!"".equals(meetingPerson) && meetingPerson!=null){
			sb.append(" and m.person like '%");
			sb.append(meetingPerson);
			sb.append("%' ");
		}
		if(!"".equals(guestName) && guestName!=null){
			sb.append(" and m.guestname like '%");
			sb.append(guestName);
			sb.append("%' ");
		}
		if(!"".equals(deptmentName) && deptmentName!=null){
			sb.append(" and m.departmentname like '%");
			sb.append(deptmentName);
			sb.append("%' ");
		}
		if(!"".equals(meetingType) && meetingType!=null){
			sb.append(" and m.type ='");
			sb.append(meetingType);
			sb.append("' ");
		}
		if(!"".equals(meetingState) && meetingState!=null){
			sb.append(" and m.meetingstate ='");
			sb.append(meetingState);
			sb.append("' ");
		}
		if(!"".equals(startDate) && startDate!=null){
			sb.append(" and m.meetingdate >= to_date('");
			sb.append(startDate);
			sb.append("', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(!"".equals(endDate) && endDate!=null){
			sb.append(" and m.meetingdate <= to_date('");
			sb.append(endDate);
			sb.append("', 'YYYY-MM-DD HH24:MI:SS')");
		}
		// 数据装载到request中，将request转发到JSP页面上做展示，页面取数据的键为listData
		request.setAttribute("listData", md.excuteHQL(sb.toString()));
		RequestDispatcher rd = request.getRequestDispatcher("meeting_list.jsp");
		rd.forward(request, response);
	}
}









